<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="备份文件" align="center" prop="connectName" />
      <el-table-column label="文件大小" align="center" prop="host" />
      <el-table-column
        label="备份时间"
        align="center"
        prop="createTime"
        width="180"
        :formatter="dateFormatter"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="danger"
            @click="handleBackup(scope.row.id)"
            v-hasPermi="['cms:db-backup:backup']"
          >
            下载
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleRestore(scope.row.id)"
            v-hasPermi="['cms:db-backup:restore']"
          >
            还原
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cms:db-backup:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>

</template>
<script lang="ts" setup>
import { dateFormatter } from '@/utils/formatTime'
import * as DataBackupApi from '@/api/infra/dbbackup'

defineOptions({ name: 'InfraDataSourceConfig' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据

/** 查询列表 */
const getList = async (id:number) => {
  loading.value = true
  try {
    list.value = await DataBackupApi.getDbBackupFiles(id)
  } finally {
    loading.value = false
  }
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}
/** 备份按钮操作 */
const handleBackup = async (id: number) => {
  try {
    // 备份的二次确认
    await message.confirm('确认备份吗?')
    // 发起备份
    await DataBackupApi.dbBackup(id)
    message.success(t('common.success'))
    // 刷新列表
    await getList(id)
  } catch {}
}
/** 还原按钮操作 */
const handleRestore = async (id: number) => {
  try {
    // 还原的二次确认
    await message.confirm('确认还原吗?')
    // 发起还原
    await DataBackupApi.dbRestore(id)
    message.success(t('common.success'))
    // 刷新列表
    await getList(id)
  } catch {}
}
/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DataBackupApi.deleteDataSourceConfig(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
